<?php

namespace app\index\controller;

use app\common\model\log\PushMsgLogs as PushMsgLogsModel;
use app\common\model\jsm\Mps as MpsModel;
use app\index\service\wechatprocess\WechatScanProcessorService;
use app\index\service\wechatprocess\WechatMsgProcessorService;
use app\index\service\wechatprocess\WechatSubscribeProcessorService;
use app\index\service\wechatprocess\WechatShakeProcessorService;
use app\common\arvato\CommonPush;

/**
 * 微信进程处理
 */
class Wechatprocess extends \app\common\controller\AuthBase {

    private $_message = null;

    public function __construct() {

        parent::__construct();

        //过滤参数，以及记录日志
        $this->filterParams();
    }

    /**
     * 过滤必须的参数
     */
    private function filterParams() {
        $param_list = [
            'openid' => 'string',
            'app_id' => 'string',
            'msg_type' => 'string',
            'xml' => 'string',
        ];
        $params = $this->input();
        $this->diffParam($param_list, $params);

        $this->_message = $params;
        
        return true;
    }

    /**
     * 统一的网关程序
     */
    public function gateway() {

        $type = $this->_message['msg_type'];
        $XmlArr = (array) simplexml_load_string($this->_message['xml'], 'SimpleXMLElement', LIBXML_NOCDATA);
        //将解析结果直接存储在message中，以便于测试，后续直接调用即可
        $this->_message['XmlArr'] = $XmlArr;

        if (!is_array($XmlArr) || empty($XmlArr)) {
            return $this->output([], "the xml is not array");
        }
        //扫码
        if ($type == "event" && $XmlArr['Event'] == 'SCAN') {
            return $this->_wechatScanProcessor();
        }
        //消息
        if ($type == "text" || $type == "image") {
            return $this->_wechatMsgProcessor();
        }
        //关注
        if ($type == "event" && $XmlArr['Event'] == 'subscribe') {
            //判断是否扫描的带参二维码
            if (!empty($XmlArr['EventKey']) &&
                    stristr($XmlArr['EventKey'], 'qrscene_')) {
                //为带有扫码场景值的关注事件做注册签到

                return $this->_wechatSubscribeScanProcessor();
            } else {
                //普通关注
                return $this->_wechatSubscribeProcessor();
            }
        }
        //取消关注
        if ($type == "event" && $XmlArr['Event'] == 'unsubscribe') {
            $this->_unsubscribeProcessor();
        }
        //摇一摇时触发事件
        if ($type == "event" && $XmlArr['Event'] == 'ShakearoundUserShake') {

            return $this->_wechatShakeProcessor();
        }

        return $this->output([], "Sorry, I don't know what you want me to do!");
    }

    //取消关注的处理
    private function _unsubscribeProcessor() {
        $commonPush = new CommonPush();
        $commonPush->pushToRedis(['openid' => $this->_message['openid']], 'removeFans');
    }

    /**
     * 扫码程序 $this->_message
     */
    private function _wechatScanProcessor() {
        $mpsModel = new MpsModel();
        $mps = $mpsModel->getMpsByAppId($this->_message['app_id'], true);
        if (empty($mps)) {
            return $this->output([], "the app_id don't find in mps table");
        }

        $wechatScanProcessorService = new WechatScanProcessorService($mps, $this->_message);
        $wechatScanProcessor = $wechatScanProcessorService->DealScan();

        if ($wechatScanProcessor === true) {
            return $this->output('scan processor');
        }
        return $this->output([], $wechatScanProcessor['msg']);
    }

    /**
     * 扫码关注事件
     */
    private function _wechatSubscribeScanProcessor() {
        $mpsModel = new MpsModel();
        $mps = $mpsModel->getMpsByAppId($this->_message['app_id'], true);
        if (empty($mps)) {
            return $this->output([], "the app_id don't find in mps table");
        }

        $wechatMsgProcessorService = new WechatSubscribeProcessorService($mps, $this->_message);
        $wechatMsgProcessor = $wechatMsgProcessorService->DealSubscribe();
        if ($wechatMsgProcessor !== true) {
            return $this->output([], $wechatMsgProcessor['msg']);
        }

        $wechatScanProcessorService = new WechatScanProcessorService($mps, $this->_message);
        $wechatScanProcessor = $wechatScanProcessorService->DealScan();
        if ($wechatScanProcessor !== true) {
            return $this->output([], $wechatScanProcessor['msg']);
        }

        return $this->output('scan subscribe processor');
    }

    /**
     * 消息处理
     */
    private function _wechatMsgProcessor() {

        $mpsModel = new MpsModel();
        $mps = $mpsModel->getMpsByAppId($this->_message['app_id'], true);
        if (empty($mps)) {
            return $this->output([], "the app_id don't find in mps table");
        }

        $wechatMsgProcessorService = new WechatMsgProcessorService($mps, $this->_message);
        $wechatMsgProcessor = $wechatMsgProcessorService->DealMsg();

        if ($wechatMsgProcessor === true) {
            return $this->output('msg processor');
        }
        return $this->output([], $wechatMsgProcessor['msg']);
    }

    /**
     * 关注处理
     */
    private function _wechatSubscribeProcessor() {
        $mpsModel = new MpsModel();
        $mps = $mpsModel->getMpsByAppId($this->_message['app_id'], true);
        if (empty($mps)) {
            return $this->output([], "the app_id don't find in mps table");
        }

        $wechatMsgProcessorService = new WechatSubscribeProcessorService($mps, $this->_message);
        $wechatMsgProcessor = $wechatMsgProcessorService->DealSubscribe();

        if ($wechatMsgProcessor === true) {
            return $this->output('subscribe processor');
        }
        return $this->output([], $wechatMsgProcessor['msg']);
    }

    /**
     * 摇一摇相关处理
     */
    private function _wechatShakeProcessor() {
        $mpsModel = new MpsModel();
        $mps = $mpsModel->getMpsByAppId($this->_message['app_id'], true);
        if (empty($mps)) {
            return $this->output([], "the app_id don't find in mps table");
        }

        $wechatMsgProcessorService = new WechatShakeProcessorService($mps, $this->_message);
        $wechatMsgProcessor = $wechatMsgProcessorService->DealShake();

        if ($wechatMsgProcessor === true) {
            return $this->output('subscribe processor');
        }
        return $this->output([], $wechatMsgProcessor['msg']);
    }

}
